package com.ming.controller;

import com.ming.pojo.Dept;
import com.ming.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dept")
public class DeptProviderController {
    @Autowired
    private DeptService deptService;


    @HystrixCommand(fallbackMethod = "hystrixQueryById")
    @GetMapping("/get/{deptno}")
    public Dept queryById(@PathVariable("deptno") Long deptno) {
        Dept dept = deptService.queryById(deptno);
        if (dept == null) {
            throw new RuntimeException("有错误");
        }
        return dept;
    }

    //备选方法
    public Dept hystrixQueryById(Long deptno) {
        Dept dept = new Dept("不存在").setDeptno(deptno).setDb_source("不存在");
        return dept;
    }


}
